package com.breaking.second.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.breaking.second.application.EscortApplication;
import com.breaking.second.databinding.ActivityEditUserBinding;
import com.breaking.second.entity.BookEntity;
import com.breaking.second.entity.UserLoginEntity;
import com.breaking.second.utils.SecurePreferencesUtil;
import com.breaking.second.utils.engine.GlideEngine;
import com.breaking.second.utils.okhttp.OkhttpUtil;
import com.bumptech.glide.Glide;
import com.luck.picture.lib.basic.PictureSelector;
import com.luck.picture.lib.config.SelectMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.interfaces.OnResultCallbackListener;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class EditUserActivity extends BaseActivity<ActivityEditUserBinding> implements OnResultCallbackListener<LocalMedia> {
    private String file;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }

    private void initView() {
        binding.image.setOnClickListener(this::image);
        binding.sava.setOnClickListener(this::sava);
    }

    private void sava(View view) {
        List<File> files = new ArrayList<>();
        files.add(new File(file));
        OkhttpUtil.uploadImage(files, new OkhttpUtil.CallbackListener(this::uploadImage));
    }

    private void uploadImage(BookEntity bookEntity) {
        if (bookEntity.isCode() && bookEntity.getData() instanceof JSONObject data) {
            List<String> image = data.getList("imageUrlList", String.class);
            if (!image.isEmpty()) {
                savaUser(image.get(0));
            }
        }
    }

    private void savaUser(String path) {
        UserLoginEntity userLoginEntity = EscortApplication.getUserLoginEntity();
        if (userLoginEntity != null) {
            userLoginEntity.setAvatar(path);
            SecurePreferencesUtil securePreferencesUtil = new SecurePreferencesUtil(UserLoginEntity.class.getName());
            securePreferencesUtil.saveEncryptedString(this, "data", JSON.toJSONString(userLoginEntity));
            EscortApplication.setUserLoginEntity(userLoginEntity);
        }

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("avatar", path);
        jsonObject.put("userId", pageableVo.getUserId());
        OkhttpUtil.Post("user/update", jsonObject, new OkhttpUtil.CallbackListener(this::savaUser));
    }

    private void savaUser(BookEntity bookEntity) {
        Toast.makeText(this, bookEntity.getMsg(), Toast.LENGTH_SHORT).show();
        if (bookEntity.isCode()) {
            finish();
        }
    }

    private void image(View view) {
        PictureSelector.create(this)
                .openGallery(SelectMimeType.ofImage())
                .setImageEngine(GlideEngine.createGlideEngine())
                .forResult(this);
    }

    @Override
    public void onResult(ArrayList<LocalMedia> result) {
        if (result.size() > 1) {
            Toast.makeText(this, "不能大于一张", Toast.LENGTH_SHORT).show();
            return;
        }
        LocalMedia localMedia = result.get(0);
        this.file = localMedia.getRealPath();
        Glide.with(this).load(new File(file)).into(binding.image);
    }

    @Override
    public void onCancel() {

    }
}
